import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.*;
import java.io.IOException;

import javax.swing.*;

public class MainFrame extends JFrame implements KeyListener, ActionListener{

	private int screenHeight = 0;
	private int screenWidth = 0;
	private JMenuItem newFile; 
	private JMenuItem saveAs;
	private JMenuItem print;
	private JMenuItem exit;
	private JMenuItem genHelp;
	private JMenuItem aboutHelp;
	private JMenuItem windowTest;
	private JMenuItem sphereCam;
	private JMenuItem povRay;
	private JMenuItem blackBackground;
	private JMenuItem redBackground;
	private JMenuItem yellowBackground;
	private JMenuItem greenBackground;
	private JMenuItem cyanBackground;
	private JMenuItem blueBackground;
	private JMenuItem magentaBackground;
	private JMenuItem customBackground;
	private JMenuItem blackLineColor;
	private JMenuItem redLineColor;
	private JMenuItem yellowLineColor;
	private JMenuItem greenLineColor;
	private JMenuItem cyanLineColor;
	private JMenuItem blueLineColor;
	private JMenuItem magentaLineColor;
	private JMenuItem customLineColor;
	private TinCDCanvas canvas;
	
	public MainFrame(String windowName)
	{
		super(windowName);
	
		JMenuBar menuBar = new JMenuBar();
		
		menuBar.add(setFileBar());
		menuBar.add(threeDBar());
		menuBar.add(setHelpBar());
		menuBar.add(setDisplayOptions());
		setJMenuBar(menuBar);
		
		
		getScreenSize();
	
		addKeyListener(this);
		this.setVisible(true);
		this.setSize(800,600);
		this.setLocation(screenWidth,0);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	public JMenu setFileBar()
	{
		JMenu fileMenu = new JMenu("File");
		newFile = new JMenuItem("New");
		newFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,ActionEvent.CTRL_MASK));
		newFile.addActionListener(this);
		fileMenu.add(newFile);
		
		saveAs = new JMenuItem("Save As");
		saveAs.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK));
		saveAs.addActionListener(this);
		fileMenu.add(saveAs);
		
		print = new JMenuItem("Print");
		print.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,ActionEvent.CTRL_MASK));
		print.addActionListener(this);
		fileMenu.add(print);
		
		exit = new JMenuItem("Exit TinCD");
		exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,ActionEvent.CTRL_MASK));
		exit.addActionListener(this);
		fileMenu.add(exit);
		
		return fileMenu;
	}
	
	public JMenu setHelpBar()
	{
		JMenu helpMenu = new JMenu("Help");
		
		genHelp = new JMenuItem("General Help");
		genHelp.setAccelerator(KeyStroke.getKeyStroke("F1"));
		genHelp.addActionListener(this);
		helpMenu.add(genHelp);
		
		aboutHelp = new JMenuItem("About TinCD");
		aboutHelp.setAccelerator(KeyStroke.getKeyStroke("F2"));
		aboutHelp.addActionListener(this);
		helpMenu.add(aboutHelp);
		
		return helpMenu;
	}
	
	public JMenu threeDBar()
	{
		JMenu threeD = new JMenu("3D Drawing");
		
		windowTest = new JMenuItem("Window Test");
		windowTest.setAccelerator(KeyStroke.getKeyStroke("F3"));
		windowTest.addActionListener(this);
		threeD.add(windowTest);
		
		sphereCam = new JMenuItem("Spherical Camera Test");
		sphereCam.setAccelerator(KeyStroke.getKeyStroke("F4"));
		sphereCam.addActionListener(this);
		threeD.add(sphereCam);
		
		povRay = new JMenuItem("Export and Run in POVRay");
		povRay.setAccelerator(KeyStroke.getKeyStroke("F5"));
		povRay.addActionListener(this);
		threeD.add(povRay);
		
		return threeD;
		
	}
	
	public JMenu setDisplayOptions()
	{
		
		/* I may want to implement a color picker for this.
		 * User customization is always very nice.
		 * Default colors though should be:
		 * Red, Yellow, Green, Cyan, Blue, Pink/Magenta
		 * 
		 * I may want to initialize this as an array for ease of assignment!!!
		 * Side note: I may want to old off on the line color assignment because
		 * of the proposed segment tool/lasso. I may want every object to have their own
		 * color object!
		 * 
		 * Background should be ok though.
		 */
		
		/*Refactor with arrays of objects. This would be just lovely!
		 * 
		 */
		
		JMenu displayOptions = new JMenu("Display Options");
		
		JMenu backgroundColor = new JMenu("Background Color");
		blackBackground = new JMenuItem("Black");
		redBackground = new JMenuItem("Red");
		yellowBackground = new JMenuItem("Yellow");
		greenBackground = new JMenuItem("Green");
		cyanBackground = new JMenuItem("Cyan");
		blueBackground = new JMenuItem("Blue");
		magentaBackground = new JMenuItem("Magenta");
		customBackground = new JMenuItem("Custom Color...");
		
		blackBackground.addActionListener(this);
		redBackground.addActionListener(this);
		yellowBackground.addActionListener(this);
		greenBackground.addActionListener(this);
		cyanBackground.addActionListener(this);
		blueBackground.addActionListener(this);
		magentaBackground.addActionListener(this);
		customBackground.addActionListener(this);
		
		backgroundColor.add(blackBackground);
		backgroundColor.add(redBackground);
		backgroundColor.add(yellowBackground);
		backgroundColor.add(greenBackground);
		backgroundColor.add(cyanBackground);
		backgroundColor.add(blueBackground);
		backgroundColor.add(magentaBackground);
		backgroundColor.add(customBackground);
		
		displayOptions.add(backgroundColor);
		
		JMenu lineColor = new JMenu ("Line Color");
		blackLineColor = new JMenuItem("Black");
		redLineColor = new JMenuItem("Red");
		yellowLineColor = new JMenuItem("Yellow");
		greenLineColor = new JMenuItem("Green");
		cyanLineColor = new JMenuItem("Cyan");
		blueLineColor = new JMenuItem("Blue");
		magentaLineColor = new JMenuItem("Magenta");
		customLineColor = new JMenuItem("Custom Color...");
		
		blackLineColor.addActionListener(this);
		redLineColor.addActionListener(this);
		yellowLineColor.addActionListener(this);
		greenLineColor.addActionListener(this);
		cyanLineColor.addActionListener(this);
		blueLineColor.addActionListener(this);
		magentaLineColor.addActionListener(this);
		customLineColor.addActionListener(this);
		
		lineColor.add(blackLineColor);
		lineColor.add(redLineColor);
		lineColor.add(yellowLineColor);
		lineColor.add(greenLineColor);
		lineColor.add(cyanLineColor);
		lineColor.add(blueLineColor);
		lineColor.add(magentaLineColor);
		lineColor.add(customLineColor);
		displayOptions.add(lineColor);
		
		return displayOptions;
	}
	
	public void getCanvas(TinCDCanvas twoDCanvas)
	{
		canvas = twoDCanvas;
		//canvas.testColor = new Color(1.0f,1.0f,0.0f);
	}
	
	public void getScreenSize()
	{
		Toolkit javaToolKit = Toolkit.getDefaultToolkit();
		Dimension screenSize = javaToolKit.getScreenSize();
		screenHeight = screenSize.height;
		screenWidth = screenSize.width/4;
		
	}

	public void fileMenuHandler(int pressed, int modifier)
	{
		
		if ( pressed == KeyEvent.VK_N && (modifier == KeyEvent.CTRL_MASK))
			{	
	     		canvas.clearJPanel();
	 
			}
		
		if ( pressed == KeyEvent.VK_S && (modifier == KeyEvent.CTRL_MASK))
			{	
	    		System.out.println("Save as");
			}
		
		if ( pressed == KeyEvent.VK_P && (modifier == KeyEvent.CTRL_MASK))
			{	
	    		System.out.println("Print");
			}
		
		if ( pressed == KeyEvent.VK_Q && (modifier == KeyEvent.CTRL_MASK))
			{	
	    		System.exit(0);
			}
					
	}
	
	public void helpMenuHandler(int pressed)
	{
		if (pressed == KeyEvent.VK_F1)
		{
	    	
	    	
		}
		
		if (pressed == KeyEvent.VK_F2)
		{
	    	System.out.println("About TinCD");
	    	
		}
	}
	
	public void initJOGL(int pressed)
	{
	    if (pressed == KeyEvent.VK_F3)
		{
	    	JOGLFrame JOGLWin = new JOGLFrame(1);
	    	
		}
	    
	    if (pressed == KeyEvent.VK_F4)
	    {
	    	JOGLFrame JOGLWin = new JOGLFrame(0);
	    }
		
	}
	
	public void initPOVRay (int pressed) throws IOException
	{
		if (pressed == KeyEvent.VK_F5)
		{
			POVRay initPOV = new POVRay();
		}
	}

	public void keyPressed(KeyEvent event) {
		
		int keyCode = event.getKeyCode();
		int modifier = event.getModifiers();
		
		fileMenuHandler(keyCode,modifier);
		helpMenuHandler(keyCode);
		
		initJOGL(keyCode);
		try {
			initPOVRay(keyCode);
		} catch (IOException e) {
			//Dialog pane here to display error print out for now
			System.out.println("POVRay failed to initialize. Please check your PATH variable and" +
					"filename!");
			e.printStackTrace();
		}
	}

	public void keyReleased(KeyEvent arg0) {
				
	}

	public void keyTyped(KeyEvent event) {
		
						
	}

	
	public void menuMouse(JMenuItem e)
	{
		
		/*Refactor this with a switch statement for e
		 * 
		 */
		if(e == newFile)
		{
			canvas.clearJPanel();
		}
		if (e == saveAs)
		{
			//stub
		}
		if (e == print)
		{
			//stub
		}
		if(e == exit)
		{
			System.exit(0);
		}
		
		if(e == blackBackground)
		{
			canvas.setBackgroundColor(new Color(0.0f,0.0f,0.0f));
		}
		
		if (e == redBackground)
		{
			canvas.setBackgroundColor(new Color(1.0f,0.0f,0.0f));
		}
		if(e == yellowBackground)
		{
			canvas.setBackgroundColor(new Color(1.0f,1.0f,0.0f));
		}
		if(e == greenBackground)
		{
			canvas.setBackgroundColor(new Color(0.0f,1.0f,0.0f));
		}
		if(e == cyanBackground)
		{
			canvas.setBackgroundColor(new Color(0.0f,1.0f,1.0f));
		}
		if(e == blueBackground)
		{
			canvas.setBackgroundColor(new Color(0.0f,0.0f,1.0f));
		}
		
		if(e == magentaBackground)
		{
			canvas.setBackgroundColor(new Color(1.0f,0.0f,1.0f));
		}
		if(e == customBackground)
		{
			//Add Custom Color Picker Here!
		}
		if(e == windowTest)
		{
			JOGLFrame JOGLWin = new JOGLFrame(1);
		}
		if(e == sphereCam)
		{
			JOGLFrame JOGLWin = new JOGLFrame(0);
		}
		if(e == povRay)
		{
			try {
				POVRay initPOV = new POVRay();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
		if(e == genHelp)
		{
			//stub
		}
		
		if(e == aboutHelp)
		{
			//stub
		}
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		
		JMenuItem itemClicked = (JMenuItem) e.getSource();
		menuMouse(itemClicked);
		
	}
}